#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
#include <cstdlib>
#define LL long long
#define ldb long double
const ldb eps = 1e-9;
const ldb pi = fabsl(atan2(0.0, -1.0));
const int INF = 0x7f7f7f7f;
const LL LINF = 1ll << 60;
const ldb LDINF = 1e42;
#define nextLine() {int c = 0; while ((c = getchar()) != 10 && c != EOF);}
#define fill(a, c) memset(a, c, sizeof a)
#define debug(a) cerr << #a << " " << a << " ";
#define debugl(a) cerr << #a << " " << a << "\n";
#define addEdge(a, b) next[edges] = first[a]; first[a] = edges; end[edges] = b; edges++;
using namespace std;

LL l, r;
int t;
const int M = 504;
int globmark;
LL dp[1 << 8][M][20][2][3];
int pre[1 << 8][M][2];
bool was[1 << 8][M][20][2][3];
int a[20];
int n;


void Load()
{
    cin >> l >> r;
}

int check(int mask, int m, int m5)
{
	int &res = pre[mask][m][m5];
	if (res != -1) return res;
    int i;
    res = 1;
    for (i = 0; res && i < 8; i++)
    {
        if ((mask & (1 << i)) != 0)
        {
            i += 2;
            if (i == 5 && m5 == 0) res = 0;
            else if (m % i != 0) res = 0; 
            i -= 2;
        }
    }
    return res;
}

LL f(int i, int mask, int m, int m5, int less)
{
    if (i == n)
    {
        if (less == 2) return 0;
        else
        {
        	//debug(mask); debug(m); debug(m5);  cerr << check(mask, m, m5) << "\n";
            if (check(mask, m, m5)) return 1;  
            else return 0;
        }
    }
    LL &res = dp[mask][m][i][m5][less];
    if (was[mask][m][i][m5][less]) return res;
	was[mask][m][i][m5][less] = 1;
    res = 0;
    if (check(mask, m, m5))
        res++;
    int nless, nmask, nm, nm5;
    int j;
    for (j = 0; j <= 9; j++)
    {
        nless = less;
        nmask = mask;
        if (less == 0 && j < a[i]) nless = 1;
        if (less == 0 && j > a[i]) nless = 2;

        if (j >= 2)
            nmask = mask | (1 << (j - 2));
        nm = (m * 10 + j) % M;
        nm5 = (j == 0) || (j == 5);
        res += f(i + 1, nmask, nm, nm5, nless);
    }
    return res;
}

LL count(LL N)
{
    if (N == 0) return 0;
    n = 0;
    while (N > 0)
    {
        a[n++] = N % 10;
        N /= 10;
    }
    reverse(a, a + n);
    memset(was, 0, sizeof(was));
    LL res = 0;
    int nless, nmask;
    int i;
    for (i = 1; i <= 9; i++)
    {
        if (i < a[0]) nless = 1;
        else if (i == a[0]) nless = 0;
        else nless = 2;
        nmask = 0;
        if (i >= 2) nmask = (1 << (i - 2));
        res += f(1, nmask, i % M, i == 5, nless);
    }
    return res;
}

void Solve()
{
	LL a = count(r);
	LL b = count(l - 1);
    cout << a - b << "\n";
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    #endif
    cin >> t;
    int i;
    fill(pre, -1);
    for (i = 0; i < t; i++)
    {
        Load();
        Solve();
    }
    return 0;
}
